package com.hl.controller;

import com.github.pagehelper.PageInfo;
import com.hl.domain.*;
import com.hl.service.CourseService;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    

    @GetMapping("/queryAll")
    public R selectAll(Integer pageNum,Integer pageSize){
        Map<String, Course> map = courseService.selectAll(pageNum, pageSize);
        return R.success(map);
    }

    //多条件动态查询
    @GetMapping("/queryAllLike")
    public R selectAllSql(String  courseName, String  type, String  consultName, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createtime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date updatetime,Integer pageNum,Integer pageSize){
        System.out.println("时间1： "+createtime);
        System.out.println("时间2： "+updatetime);

        Map<String, Course> map = courseService.selectAllSql(courseName, type, consultName, createtime, updatetime, pageNum, pageSize);

        return R.success(map);
    }


    @PostMapping("/upload")
    public R  upload(MultipartFile file) throws IOException {
        //上传到D盘
        String  path="D:\\picture\\";

        String originalFilename = file.getOriginalFilename();
        String ext=originalFilename.substring(originalFilename.lastIndexOf("."));
        String  filename= UUID.randomUUID().toString()+ext;
        path+=filename;

        file.transferTo(new File(path));

        return  R.success(filename);
    }

    @GetMapping("/down")
    public  R   down(String  filename, HttpServletResponse response) throws IOException {
        //下载到D盘
        FileInputStream in=new FileInputStream("d:\\picture\\"+filename);
        ServletOutputStream out = response.getOutputStream();

        byte[]  b=new  byte[1024];
        int  i=in.read(b);
        while(i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        out.close();
        in.close();
        return R.success(filename);
    }


    //查询所有课程类型
    @RequestMapping("/queryAllType")
    public R queryAll() {
        List<Coursetype> coursetypes = courseService.queryAll();
        return R.success(coursetypes);
    }


    //查询所有咨询师
    @RequestMapping("/queryAllTeacher")
    public R selectAllTeacher() {
        List<Teacher> teachers = courseService.selectAllTeacher();
        return R.success(teachers);
    }

    //添加课程
    @PostMapping("/addCourse")
    public R add(Course course) {
        int row = courseService.add(course);
        return R.success(row);
    }


    //根据id修改课程
    @PutMapping("/updateCourse")
    public R update(Course course) {
        int row = courseService.update(course);
        return R.success(row);
    }

    //根据id删除课程
    @DeleteMapping("/deleteCourse")
    public R delete(Long id){
        System.out.println(id);
        int row = courseService.delete(id);
        return R.success(row);
    }



    //课程首页轮播图----
    @RequestMapping("/lunbo")
    public R selectCourseLunbo(Integer pageNum, Integer pageSize){
        Map<String, Course> courseLunbo = courseService.selectCourseLunbo(pageNum, pageSize);
        return R.success(courseLunbo);
    }

    //===========暂时不用============
//    @GetMapping("/aisort")
//    public PageInfo<Course> getCourses(
//            @RequestParam(required = false) String priceRange,
//            @RequestParam(required = false, defaultValue = "createTime") String orderBy,
//            @RequestParam(required = false, defaultValue = "desc") String orderDirection,
//            @RequestParam(required = false,defaultValue = "2") Integer typeId,
//            @RequestParam(required = false) Integer minReadingQuantity,
//            @RequestParam(defaultValue = "1") int pageNum,
//            @RequestParam(defaultValue = "10") int pageSize) {
//
//        return courseService.getCoursesByCondition(priceRange, orderBy, orderDirection, typeId, minReadingQuantity, pageNum, pageSize);
//    }


//    通过联表查询所有
    @GetMapping("/selectAllByAssociation")
    public R  selectAllByAssociation(){
        return R.success(courseService.selectAllByAssociation());
    }
    @GetMapping("/selectAllByAssociation2")
    public R  selectAllByAssociation2(){
        return R.success(courseService.selectAllByAssociation2());
    }
    //根据课程id改变状态
    @PostMapping("/modify")
    public R modifyStatus(Long id){
        System.out.println("---------------"+id);
        return R.success(courseService.modifyStatus(id));
    }

    @GetMapping("/sort")
    public R listCourses(Integer sortType, Integer orderDirection, Integer priceRangeType) {
        //封装查询条件
        CourseQueryDTO queryDTO = new CourseQueryDTO();
        //priceRange 为0-5的数字
        //0免费....依此类推

        // 处理排序类型   --1表示阅读量   3表示价格
        if (sortType != null) {
            switch (sortType) {
                case 1:
                    queryDTO.setOrderBy("readingQuantity");
                    break;
                case 3:
                    queryDTO.setOrderBy("courseprice");
                    break;
                default:
                    queryDTO.setOrderBy("id");
                    break;
            }
        } else {
            queryDTO.setOrderBy("id");
        }

        // 处理排序方向
        queryDTO.setOrderDirection(orderDirection != null && orderDirection == 0 ? "asc" : "desc");

        // 处理价格区间
        queryDTO.setPriceRangeType(priceRangeType);
        List<CourseQuerySort> courses = courseService.listCourses(queryDTO);

        return R.success(courses);
    }
}
